package com.zx._09_架构.rpc演变过程.rpc_01;

import com.zx.common.entity.UserRpcDto;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Client {

    public static void main(final String[] args) throws Exception {
        final Socket socket = new Socket("localhost", 8888);
        final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        final DataOutputStream output = new DataOutputStream(outputStream);
        output.writeInt(10);

        final OutputStream socketOut = socket.getOutputStream();
        socketOut.write(outputStream.toByteArray());
        socketOut.flush();

        final InputStream inputStream = socket.getInputStream();
        final DataInputStream input = new DataInputStream(inputStream);
        final long id = input.readLong();
        final String name = input.readUTF();
        final UserRpcDto userRpcDto = new UserRpcDto(id, name);

        log.info("consumer = {}", userRpcDto.toString());
        outputStream.close();
        socket.close();
    }
}
